package com.flyim.framework.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;


/**
 * @Package: com.flyim.framework.netty
 * @ClassName: WebSocketInitializer.java
 * @Description: WebSocket 协议处理链
 * @author: zhengxiaolong
 * @date: 2025/2/21 14:14
 */
public class WebSocketInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        // HTTP 协议编解码器（WebSocket 基于 HTTP）
        pipeline.addLast(new HttpServerCodec());
        // 聚合 HTTP 请求内容为 FullHttpRequest/FullHttpResponse
        pipeline.addLast(new HttpObjectAggregator(65536));
        // WebSocket 协议处理器，指定路径为 /ws
        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
        // 自定义消息处理器（处理业务逻辑）
        pipeline.addLast(new IMHandler());
    }
}